package com.example.service.impl;

import com.example.dao.ArticleRepository;
import com.example.pojo.Article;
import com.example.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    ArticleRepository dao;

    @Override
    public List<Article> selectAll() {
        List<Article> all = dao.findAll();
        return all;
    }

    @Override
    public Page<Article> selectPage(Integer page, Integer size) {
        Sort sort = Sort.by(Sort.Direction.ASC, "id");
        PageRequest pageRequest = PageRequest.of(page, size, sort);
        Pageable first = pageRequest.first();
        Page<Article> all = dao.findAll(pageRequest);
        return all;
    }
}
